﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using DataAccessLayer;
using DataAccessLayer.DAL;
using Common;

namespace BestBuy.SellerViews
{
    public partial class AdvertiseProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            User user = UserDAL.GetUserByUsername(HttpContext.Current.User.Identity.Name);
            if (user == null)
            {
                Response.Redirect("~");
            }
            if (!IsPostBack)
            {
                LoadTreeView();
            }
        }

        private void LoadTreeView()
        {
            tvCategories.Nodes.Clear();
            List<ProductCategory> list = ManagerDAL.GetAllCategories();
            TreeNode root = new TreeNode();
            root.Value = "0";
            root.Text = "Category";
            CreateCategory(list, root, null);
            tvCategories.Nodes.Add(root);
        }

        private void CreateCategory(List<ProductCategory> list, TreeNode parent, int? parentID)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].ParentID == parentID)
                {
                    TreeNode treenode = new TreeNode();
                    treenode.Value = list[i].ID.ToString();
                    treenode.Text = list[i].Name;
                    CreateCategory(list, treenode, list[i].ID);
                    parent.ChildNodes.Add(treenode);
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (tvCategories.CheckedNodes.Count == 1)
                {
                    int userID = UserDAL.GetUserIDByUsername(HttpContext.Current.User.Identity.Name);
                    DataAccessLayer.AdvertiseProduct adv = new DataAccessLayer.AdvertiseProduct();
                    adv.Title = txtTitle.Text.Trim();
                    adv.CategoryID = int.Parse(tvCategories.CheckedNodes[0].Value.ToString());
                    adv.CreatedDate = DateTime.Now;
                    adv.Description = txtDescription.Text.Trim();
                    adv.Quantity = int.Parse(txtQuantity.Text);
                    adv.SellerID = userID;
                    adv.Status = (int)AdvertiseProductStatus.waitting;
                    List<string> listImage = FileUpload();
                    SellerDAL.AddAdvertiseProduct(adv, listImage);

                    AdvertiseProductMessage.Show("Success", UserControls.MessagePanel.MessageType.Success);
                }
            }
            catch
            {
                AdvertiseProductMessage.Show("Please enter a valid", UserControls.MessagePanel.MessageType.Error);
            }
        }

        private List<string> FileUpload()
        {
            List<string> list = new List<string>();
            HttpFileCollection arr = Request.Files;
            for (int i = 0; i < arr.Count; i++)
            {
                HttpPostedFile file = arr[i];
                if (file.ContentLength > 0)
                {
                    string root = "~/FileUpload/";
                    string filename = Path.GetFileName(file.FileName);
                    file.SaveAs(Server.MapPath(root) + filename);
                    list.Add(root + filename);
                }
            }
            return list;
        }

    }
}